package com.kkb.study;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Operator {
    public static void main(String[] args) {
        test6();
    }
    public static void test1(){
        Consumer<Integer> consumer = (Integer n) -> {
            System.out.println(n);
        };
        consumer.accept(5);
    }

    public static void test2(){
        Consumer<Integer> consumer = System.out::println;
        consumer.accept(8);
    }

    public static void test3(){
        Student stu1 = new Student("Jack",32);
        // 输出getName；
        Supplier<String> supplier1 = () -> {
            return stu1.getName();
        };
        System.out.println(supplier1.get());

        Supplier<Integer> supplier2 = stu1::getAge;
        System.out.println(supplier2.get());
    }

    public static void test4(){
//        Student student = new Student("Seven",28);
        Supplier<String> supplier1 = () -> {
            return Student.sayHello();
        };
        System.out.println(supplier1.get());

        Supplier<String> supplier2 = Student::sayHello;
        System.out.println(supplier2.get());
    }

    public static void test5(){
        Student student = new Student("Jack",32);

        // getAge
        Function<Student,Integer> function1 = (Student stu) -> {
            return stu.getAge();
        };
        System.out.println(function1.apply(student));

        Function<Student,Integer> function2 = Student::getAge;
        System.out.println(function1.apply(student));
    }

    public static void test6(){
        // 创建无参对象。
        Supplier<Student> supplier1 = () -> {
            return new Student();
        };
        System.out.println(supplier1.get().toString());

        Supplier<Student> supplier2 = Student::new;
        System.out.println(supplier2.get().toString());

        // 创建有参对象
        Function<Object[], Student> function1 = (Object[] obj) -> {
            Student student = new Student((String)obj[0], (Integer)obj[1]);
            return student;
        };
        Object[] arr = {"Seven", 28};
        System.out.println(function1.apply(arr));

        Function<Object[], Student> function2 = Student::new;
        System.out.println(function2.apply(arr));
    }


}
